package cn.com.demo.cms;

import org.bouncycastle.cms.CMSCompressedData;
import org.bouncycastle.cms.CMSCompressedDataGenerator;
import org.bouncycastle.cms.CMSProcessableByteArray;

import java.util.Arrays;

public class CompressedDataExample {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //set up the generator
        CMSCompressedDataGenerator gen = new CMSCompressedDataGenerator();

        //compress the data
        CMSProcessableByteArray data = new CMSProcessableByteArray("Hello World!".getBytes());

        CMSCompressedData compressed = gen.generate(data, CMSCompressedDataGenerator.ZLIB);

        //re-create and uncompress the data
        compressed = new CMSCompressedData(compressed.getEncoded());

        byte[] recData = compressed.getContent();

        //compare uncompressed data to the original data
        if (Arrays.equals(recData, (byte[]) data.getContent())) {
            System.out.println("data recovery succeeded");
        } else {
            System.out.println("data recovery failed");
        }
    }

}
